package com.zzxx.controller;

import com.github.pagehelper.PageInfo;
import com.zzxx.entity.*;

import com.zzxx.exceptions.UpdateException;
import com.zzxx.service.GymService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("gym")
public class GymController {
    @Autowired
    private GymService gymService;

    @Autowired(required = false)
    private HttpServletRequest request;

    @RequestMapping("queryAll")
    public PageInfo<Gym> list1(@RequestParam(defaultValue = "1") Integer start, @RequestParam(defaultValue = "2") Integer pageSize, @RequestParam(defaultValue = "2") Integer navigatePages){
        PageInfo<Gym> pageInfo=gymService.queryAll(start, pageSize);
        pageInfo.setNavigatePages(navigatePages);
        return pageInfo;
    }

    @RequestMapping("queryById")
    public Gym list2(String id){
        Gym pageInfo=gymService.queryById(id);
        return pageInfo;
    }

    @RequestMapping(value = "findPage",method = RequestMethod.GET)
    public PageInfo<Gym> findPage(Integer pageNum, Integer pageSize){
        pageNum = pageNum==null? 1:pageNum;
        pageSize = pageSize==null? 5:pageSize;
        PageInfo<Gym> pageInfo= gymService.findPage(pageNum, pageSize);
        return pageInfo;
    }

    @RequestMapping(value = "findById/{gid}", method = RequestMethod.GET)
    public Gym findById(@PathVariable("gid") String gid){
        Gym gym = gymService.findById(gid);
        return gym;
    }

    @RequestMapping(value = "updateGym", method = RequestMethod.POST)
    public String updateById(MultipartFile uploadPic, Gym gym) throws IOException {
        Manager loginManager = (Manager) request.getSession().getAttribute("loginManager");
        if (loginManager==null) return "noLogin";
        try {
            gymService.updateById(uploadPic, gym, loginManager);
        } catch (UpdateException e) {
            return e.getMessage();
        }
        return "success";
    }

    @RequestMapping(value = "addGym", method = RequestMethod.POST)
    public String addGym(MultipartFile uploadPic, Gym gym) throws IOException {
        Manager loginManager = (Manager) request.getSession().getAttribute("loginManager");
        if (loginManager==null) return "noLogin";
        gym.setPrincipalid(loginManager.getId());
        gym.setStatus(0);
        try {
            gymService.addGym(uploadPic, gym);
        } catch (UpdateException e) {
            return e.getMessage();
        }
        return "success";
    }

    @RequestMapping(value = "delete/{gid}",method = RequestMethod.DELETE)
    public String delete(@PathVariable("gid") String gid){
        Manager loginManager = (Manager) request.getSession().getAttribute("loginManager");
        if (loginManager==null) return "noLogin";
        try {
            gymService.delete(gid,loginManager.getId());
        } catch (UpdateException e) {
            return e.getMessage();
        }
        return "success";
    }

    @RequestMapping(value = "findPower/{gid}", method = RequestMethod.GET)
    public List<ManagerAndPower> findPower(@PathVariable("gid") String gid){
        //检查有没有权限
        Manager loginManager = (Manager) request.getSession().getAttribute("loginManager");
        if (loginManager==null) return null;
        if (!gymService.checkPower(gid,loginManager.getId())) return null;
        List<ManagerPower> list = gymService.findPower(gid,loginManager.getId());
        List<ManagerAndPower> mapList = gymService.packageMap(list);
        return mapList;
    }

    @RequestMapping(value = "updatePower", method = RequestMethod.POST)
    public String updatePower(@RequestBody Map<String,List<ManagerAndPower>> map){
        List<ManagerAndPower> list = map.get("powerList");
        gymService.updatePower(list);
        return "success";
    }

    @RequestMapping(value = "removePower/{mpid}", method = RequestMethod.DELETE)
    public String removePower(@PathVariable("mpid") String mpid){
        gymService.removePower(mpid);
        return "success";
    }

    @RequestMapping(value = "addPower", method = RequestMethod.POST)
    public String addPower(@RequestBody Map<String,String> map){
        String addName = map.get("addName");
        String addPower = map.get("addPower");
        String addType = map.get("addType");
        String addPowerId = map.get("addPowerId");
        String res = gymService.addPower(addName, addPower, addType, addPowerId);
        return res;
    }
}
